﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThousandFunClient.model;
using ThousandFunClient.Util;
using System.Collections;
using System.IO.Ports;

namespace ThousandFunClient
{
    public partial class Fm_Config : Form
    {
        public Fm_Config()
        {
            InitializeComponent();
        }

        
        private void btn_back_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (!cbb_comA.Text.Equals(""))
            {
                TFConfig.GetInstance().PortConfigA.PortName = cbb_comA.Text;
            }
            if (!txt_baudrateA.Text.Equals(""))
            {
                TFConfig.GetInstance().PortConfigA.BaudRate = txt_baudrateA.Text;
            }
            if (!cbb_comB.Text.Equals(""))
            {
                TFConfig.GetInstance().PortConfigB.PortName = cbb_comB.SelectedText;
            }
            if (!txt_baudrateB.Text.Equals(""))
            {
                TFConfig.GetInstance().PortConfigB.BaudRate = txt_baudrateB.Text;
            }
            ConfigUtil.xmlUpdate();
        }

        private void Fm_Config_Load(object sender, EventArgs e)
        {
            ColumnHeader cZh = new ColumnHeader();//创建一个列
            cZh.Text = "CMD";//列名
            cZh.Width = 260;
            ColumnHeader cCh = new ColumnHeader();
            cCh.Text = "Interval";
            ColumnHeader cAh = new ColumnHeader();
            cAh.Text = "LoggerName";
            cAh.Width = 150;
            lv_main.Columns.AddRange(new ColumnHeader[] { cZh, cCh, cAh });

            lbl_portA.Text = "com:" + TFConfig.GetInstance().PortConfigA.PortName + ".BaudRate:" + TFConfig.GetInstance().PortConfigA.BaudRate;
            lbl_portB.Text = "com:" + TFConfig.GetInstance().PortConfigB.PortName + ".BaudRate:" + TFConfig.GetInstance().PortConfigB.BaudRate;
            Hashtable hc = TFConfig.GetInstance().HtBatchCMD;

            cbb_comA.Items.Clear();
            cbb_comB.Items.Clear();
            foreach (DictionaryEntry de in hc) //ht为一个Hashtable实例
            {

                SendCMDConfig sendCMDConfig = (SendCMDConfig)de.Value;

                lv_main.View = View.Details;//列的显示模式
                ListViewItem lvi = new ListViewItem(new string[] { sendCMDConfig.SendCMD, sendCMDConfig.Interval, sendCMDConfig.LoggerName }, -1);
                lv_main.Items.Add(lvi);

            }


            for (int i = 1; i < 50; i++)
            {

                try
                {
                    SP_main.PortName = "com" + i.ToString();
                    SP_main.Open();
                    SP_main.Close();
                }
                catch (Exception ec)
                {
  
                    continue;
                }
                
                String comcc = "com" + i.ToString();

                this.comboBox1.DisplayMember = "Name";
                this.comboBox1.ValueMember = "Id";

                //cbb_comA.Items.Add(new DictionaryEntry(comcc, comcc));
                //cbb_comB.Items.Add(new DictionaryEntry(comcc, comcc));
                cbb_comA.Items.Add(comcc);
                cbb_comB.Items.Add(comcc);
            }

            SP_main.DataReceived += SP_main_DataReceived;
            
        }

        void CrossThreadCall_Local(string reply)
        {
            txt_testreply.Text = reply;
        }
        public delegate void void_d(string reply);

        private void SP_main_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int int_Len = SP_main.BytesToRead;
            byte[] buf = new byte[int_Len];
            SP_main.Read(buf, 0, int_Len);
            string get = ASCIIEncoding.ASCII.GetString(buf);
            if (get != null)
            {
                Invoke(new void_d(CrossThreadCall_Local), get);
            }
        }

        private void btn_test_Click(object sender, EventArgs e)
        {
            PortUtil.CloseCom(SP_main);
            if (cbb_test.SelectedText.Equals("PortA"))
            {
                PortUtil.SetPortByConfig(SP_main, TFConfig.GetInstance().PortConfigA);
            }
            else
            {
                PortUtil.SetPortByConfig(SP_main, TFConfig.GetInstance().PortConfigB);
            }
            PortUtil.OpenCOM(SP_main);
            SP_main.Write(txt_testInput.Text);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

    }
}
